package com.xieli.invitesdk.utils

import java.math.BigInteger
import java.security.MessageDigest
import java.util.Random
/**
 * @Auther sr
 * @Date 2023/9
 * @Description 字符串工具类
 */
object StringUtils {

     var textdata = arrayListOf("陈***琳","邓***血","蔡***宜","森***凉","球***7","完***子","先***去",
         "久***留","可***人","珂***生","柚***天","放***星", "电***厂","王***得","沈***姐","余***姨",
         "小***人","冲***宝","白***红","达***硕","雨***刻","不***躲","偏***爱","理***由", "冬***仔",
         "梁***盘","㕤***h","桂***龙","维***C","替***品","咖***肉","高***英","美***店","柠***茶",
         "中***馆","美***疗", "雅***黛","T***s","V***s", "A***n","C***d","U***v","o***O","G***k",
         "T***T","W***6","P***D","6***1","2***u","8***k")

    /**
     * 轮播概率
     */
    var probabilities = mapOf(
        "30天会员" to 0.3,
        "1年会员" to 0.3,
        "终身会员" to 0.4
    )

    /**
     * 设置轮播数据
     * @param newData 设置的新的数据
     */
    fun updateTextData(newData: ArrayList<String>) {
        if (newData.isNotEmpty()) {
            textdata = newData
        }

    }
    /**
     * 生成对应概率字符串
     * @param probabilities 设置的概率列表
     */
    fun getRandomStringWithProbability(probabilities: Map<String, Double>): String {
        val random = Random()
        val totalProbability = probabilities.values.sum()
        var randomValue = random.nextDouble() * totalProbability

        for ((string, probability) in probabilities) {
            randomValue -= probability
            if (randomValue <= 0.0) {
                return string
            }
        }

        // 如果没有匹配的概率，返回空字符串或默认字符串
        return ""
    }

    /**
     * Md5加密
     * @param plainText 需要加密的字符串
     */
    fun Md5(plainText: String?): String {
        var text = plainText
        if (text == null) {
            text = ""
        }
        try {
            val md = MessageDigest.getInstance("MD5")
            md.update(text.toByteArray())
            val digest = md.digest()
            val md5 = BigInteger(1, digest).toString(16)
            return md5.padStart(32, '0')
        } catch (e: Exception) {
            throw RuntimeException("MD5加密错误: ${e.message}", e)
        }
    }
}